<?php
class RDatabase{
	public static $AR_VARS=array();
	public static $B_CONNECTED=false;
	public static function reg($key,$val,$bNumber=false){
		
	}
	public static function _query($sql=null){
		global $_CONFIG;
		if(!RDatabase::$B_CONNECTED){
			if(!mysql_connect($_CONFIG['db_host'],$_CONFIG['db_username'],$_CONFIG['db_password'])){
				
				if($_CONFIG['debug']) echo('DB ERROR: CONNECT ERROR');
				return false;
			}
			if(!mysql_select_db($_CONFIG['db_name'])){
				if($_CONFIG['debug']) echo ('DB ERROR: SELECT ERROR');
				return false;
			}
			mysql_query("SET NAMES 'utf8'");  
			mysql_query("SET CHARACTER_SET_CLIENT=utf8");  
			mysql_query("SET CHARACTER_SET_RESULTS=utf8 ");  
		
			RDatabase::$B_CONNECTED=true;
		}
		$res=mysql_query($sql);
		if(!$res){
			if($_CONFIG['debug']) echo('SQL: '.$sql.'<br/>Error: '.mysql_error());
			return false;
		}
		return $res;
	}
	public static function query($sql=''){
		foreach(RDatabase::$AR_VARS as $k=>$v){
			$sql=str_replace('$'.$k,$v);	
		}
		return RDatabase::_query($sql);
	}
}
function sql($sz=''){
	return RDatabase::query($sz);	
}
function fetch($res){
	return mysql_fetch_array($res);	
}
function filter($var,$isNumber=false){
	if($isNumber){
		if(!is_numeric($var)){
			$var=preg_replace('/\D/','',$var);
		}
	}else{
		$var=str_replace('"','""',$var);
	}
	return $var;
}
?>